home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / comm / wnos5src.zip / IPHDR.C < prev    next >
Text File  |  1993-08-09  |  4KB  |  163 lines

  1. /* IP header conversion routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include "global.h"
  5. #include "mbuf.h"
  6. #include "ip.h"
  7. #include "internet.h"
  8.  
  9. /* Convert IP header in host format to network mbuf
  10.  * If cflag != 0, take checksum from structure,
  11.  * otherwise compute it automatically.
  12.  */
  13. struct mbuf *
  14. htonip(struct ip *ip,struct mbuf *data,int cflag)
  15. {
  16.     int16 fl_offs, hdr_len = IPLEN + ip->optlen;
  17.     struct mbuf *bp = pushdown(data,hdr_len);
  18.     char *cp = bp->data;
  19.  
  20.     *cp++ = (ip->version << 4) | (hdr_len >> 2);
  21.     *cp++ = ip->tos;
  22.     cp = put16(cp,ip->length);
  23.     cp = put16(cp,ip->id);
  24.     fl_offs = ip->offset >> 3;
  25.     if(ip->flags.congest)
  26.         fl_offs |= 0x8000;
  27.     if(ip->flags.df)
  28.         fl_offs |= 0x4000;
  29.     if(ip->flags.mf)
  30.         fl_offs |= 0x2000;
  31.  
  32.     cp = put16(cp,fl_offs);
  33.     *cp++ = ip->ttl;
  34.     *cp++ = ip->protocol;
  35.     if(cflag){
  36.         /* Use checksum from host structure */
  37.         cp = put16(cp,ip->checksum);
  38.     } else {
  39.         /* Clear checksum for later recalculation */
  40.         *cp++ = 0;
  41.         *cp++ = 0;
  42.     }
  43.     cp = put32(cp,ip->source);
  44.     cp = put32(cp,ip->dest);
  45.     if(ip->optlen != 0)
  46.         memcpy(cp,ip->options,ip->optlen);
  47.  
  48.     /* If requested, recompute checksum and insert into header */
  49.     if(!cflag)
  50.         put16(&bp->data[10],cksum(NULLHEADER,bp,hdr_len));
  51.  
  52.     return bp;
  53. }
  54.  
  55. /* Extract an IP header from mbuf */
  56. int
  57. ntohip(struct ip *ip,struct mbuf **bpp)
  58. {
  59.     int16 ihl, fl_offs;
  60.     char ipbuf[IPLEN];
  61.  
  62.     if(pullup(bpp,ipbuf,IPLEN) != IPLEN)
  63.         return -1;
  64.  
  65.     ip->version = (ipbuf[0] >> 4) & 0xf;
  66.     ip->tos = ipbuf[1];
  67.     ip->length = get16(&ipbuf[2]);
  68.     ip->id = get16(&ipbuf[4]);
  69.     fl_offs = get16(&ipbuf[6]);
  70.     ip->offset = fl_offs << 3;
  71.     ip->flags.mf = (fl_offs & 0x2000) ? 1 : 0;
  72.     ip->flags.df = (fl_offs & 0x4000) ? 1 : 0;
  73.     ip->flags.congest = (fl_offs & 0x8000) ? 1 : 0;
  74.     ip->ttl = ipbuf[8];
  75.     ip->protocol = ipbuf[9];
  76.     ip->checksum = get16(&ipbuf[10]);
  77.     ip->source = get32(&ipbuf[12]);
  78.     ip->dest = get32(&ipbuf[16]);
  79.  
  80.     if((ihl = (ipbuf[0] & 0xf) << 2) < IPLEN) {
  81.         /* Bogus packet; header is too short */
  82.         return -1;
  83.     }
  84.     ip->optlen = ihl - IPLEN;
  85.     if(ip->optlen != 0)
  86.         pullup(bpp,ip->options,ip->optlen);
  87.  
  88.     return ip->optlen + IPLEN;
  89. }
  90.  
  91. /* Perform end-around-carry adjustment */
  92. static int16
  93. eac(int32 sum)    /* Carries in high order 16 bits */
  94. {
  95.     int16 csum;
  96.  
  97.     while((csum = sum >> 16) != 0)
  98.         sum = csum + (sum & 0xffffL);
  99.     return (sum & 0xffffl);    /* Chops to 16 bits */
  100. }
  101.  
  102. /* Checksum a mbuf chain, with optional pseudo-header */
  103. int16
  104. cksum(struct pseudo_header *ph,struct mbuf *m,int16 len)
  105. {
  106.     int swap = 0;
  107.     int16 cnt, total, csum1;
  108.     int32 csum, sum = 0;
  109.     char *up;
  110.  
  111.     /* Sum pseudo-header, if present */
  112.     if(ph != NULLHEADER){
  113.         sum  = hiword(ph->source);
  114.         sum += loword(ph->source);
  115.         sum += hiword(ph->dest);
  116.         sum += loword(ph->dest);
  117.         sum += uchar(ph->protocol);
  118.         sum += ph->length;
  119.     }
  120.     /* Now do each mbuf on the chain */
  121.     for(total = 0; m != NULLBUF && total < len; m = m->next) {
  122.         cnt = (int16)min(m->cnt, len - total);
  123.         up = m->data;
  124.         csum = 0;
  125.  
  126.         if(((long)up) & 1){
  127.             /* Handle odd leading byte */
  128.             if(swap) {
  129.                 csum = uchar(*up++);
  130.             } else {
  131.                 csum = (int16)(uchar(*up++) << 8);
  132.             }
  133.             cnt--;
  134.             swap = !swap;
  135.         }
  136.         if(cnt > 1){
  137.             /* Have the primitive checksumming routine do most of
  138.              * the work. At this point, up is guaranteed to be on
  139.              * a short boundary
  140.              */
  141.             csum1 = lcsum((unsigned short *)up, (int16)(cnt >> 1));
  142.             if(swap) {
  143.                 csum1 = (csum1 << 8) | (csum1 >> 8);
  144.             }
  145.             csum += csum1;
  146.         }
  147.         /* Handle odd trailing byte */
  148.         if(cnt & 1){
  149.             if(swap) {
  150.                 csum += uchar(up[--cnt]);
  151.             } else {
  152.                 csum += (int16)(uchar(up[--cnt]) << 8);
  153.             }
  154.             swap = !swap;
  155.         }
  156.         sum += csum;
  157.         total += m->cnt;
  158.     }
  159.     /* Do final end-around carry, complement and return */
  160.     return (int16)(~eac(sum) & 0xffff);
  161. }
  162.  
  163.